if(!window.mc){
	window.mc = function(){};
}
window.mc.main = function(){};

mc.main.check = function(param){
	if(!param.main){
		alert("need main id");
		return false;
	}
	if($("#" + param.main).length == 0){
		alert("bad main id "+ param.main);
		return false;
	}
	if(!param.tape){
		alert("need tape id");
		return false;
	}
	if($("#" + param.tape).length == 0){
		alert("bad tape id "+ param.tape);
		return false;
	}
	if(!param.panel){
		alert("need panel id");
		return false;
	}
	if($("#" + param.panel).length == 0){
		alert("bad panel id "+ param.panel);
		return false;
	}
	if(!param.topic){
		alert("need topic id");
		return false;
	}
	if($("#" + param.topic).length == 0){
		alert("bad topic id "+ param.topic);
		return false;
	}
	if(!param.login){
		alert("need login");
		return false;
	}
	if(!param.pass){
		alert("need pass");
		return false;
	}
	if(!param.config){
		alert("need config");
		return false;
	}
	if(!param.config.url){
		alert("need config.url");
		return false;
	}
	return true;
}
mc.main.set_setting = function(setting){
	mc.setting = setting || {};
	this.max_str = mc.setting.max_str || 1000;
	this.del_str = mc.setting.del_str || Math.floor(this.max_str / 2);
}
mc.main.init = function(param){
	var config = param.config;
	this.set_setting(param.setting);
	
	this.ping_interval = config.ping_interval || 30;
	this.http_timeout = config.http_timout || 60;
	this.login_delay = config.login_delay || 5;
	this.login_attempt = config.login_attempt || 50;
	this.login_try = 0;
	this.login_interval = -1;
	this.login_xhr = false;

	this.check_timeout = -1;
	this.check_xhr = false;
	

	this.topic = $("#" + param.topic);

	mc.template.init(config.template_path, config.template_ver);

	mc.tape.init($("#"+ param.tape));
	mc.cmd.init($("#"+ param.main));
	mc.transport.init(config.url);

	mc.panel.init($("#"+ param.panel));

	this.sid = "";
	this.login = param.login;
	this.pass = param.pass;
}
mc.main.call_exec = function(cmd){
}
mc.main.call_topic = function(text){
	mc.main.topic.html(text);
}
mc.main.after_login = function(){
	mc.main.call_check();
}
mc.main.call_check = function(){
	if(mc.main.check_xhr){
		mc.main.check_xhr.abort();
		mc.main.check_xhr = false;
	}
	if(mc.main.check_timeout != -1){
		window.clearTimeout(mc.main.check_timeout);
		mc.main.check_timeout = -1
	}
	if(!mc.main.sid || mc.main.sid == ""){
		return;
	}
	//mc.tape.put_console("call check.");
	mc.main.check_timeout = window.setTimeout(mc.main.call_check,mc.main.http_timeout*1000);
	mc.main.check_xhr = mc.transport.send_message({"cmd":"check"});
}
mc.main.call_login = function(){
	if(mc.main.login_interval == -1){
		mc.main.login_try = 0;
		mc.main.login_interval = window.setInterval(mc.main._ask_login,mc.main.login_delay*1000);
		mc.main._ask_login();
	}
}
mc.main._ask_login = function(){
	if(mc.main.login_xhr){
		mc.main.login_xhr.abort();
		mc.main.login_xhr = false;
	}
	if(mc.main.sid && mc.main.sid != ""){
		if(mc.main.login_interval != -1){
			window.clearInterval(mc.main.login_interval);
			mc.main.login_interval = -1;
		}
		return;
	}
	if(++mc.main.login_try > mc.main.login_attempt){
		if(mc.main.login_interval != -1){
			window.clearInterval(mc.main.login_interval);
			mc.main.login_interval = -1;
		}
		mc.tape.put_console("Превышено число попыток соединения.");
		return;
	}
	mc.tape.put_console("Соединяемся...");
	mc.main.login_xhr = mc.transport.send_message({"cmd":"login","login":mc.main.login,"pass":mc.main.pass});
}
